package com.cms.manager;

import java.util.ArrayList;
import java.util.List;

import com.cms.model.Message;
import com.cms.model.Tactics;
import com.cms.service.MessageService;
import com.cms.service.MessageServiceImpl;
import com.cms.service.TacticsService;
import com.cms.service.TacticsServiceImpl;

public class CmsManager {
	private static CmsManager instance = null;
	private String url = "http://m.bjforest.com/cms/m_dl.do?f=";
	public static synchronized CmsManager getInstance()
	{
		if (instance == null)
		{
			instance = new CmsManager();
		}
		return instance;
	} 
	protected CmsManager()
	{
		InitThread initThread = new InitThread();
		initThread.start();
		init();
	}
	List<Message> list = new ArrayList<Message>();
	List<Tactics> tlist = new ArrayList<Tactics>();
	public class InitThread extends Thread
	{
		@Override
		public void run() {
			while(true)
			{
				CmsManager.getInstance().init();
				try {
					sleep(5*60*1000);
				} catch (InterruptedException e) {
				}
			}
		}
	}
	protected void init()
	{
		//获得所有内容
		MessageService service  = new MessageServiceImpl();
		list = service.getAll(new Message());
		
		//获得所有的下发配置
		TacticsService tService  = new TacticsServiceImpl();
		tlist = tService.getTacticss();
	}
	
	public String getCms(String spId,String toicp,String code)
	{
		List<Message> newlist = new ArrayList<Message>();
		String languageid="";
		String  typeid="";
		String type = "";
		//精确匹配队列和指令
		for(int i =0;i<tlist.size();i++)
		{
			Tactics t = tlist.get(i);
			if(t.getSpid().equalsIgnoreCase(spId))
			{
				if(t.getToicp().equalsIgnoreCase(toicp) && t.getPaycode().equalsIgnoreCase(code))
				{
					languageid = t.getLanguageid();
					typeid = t.getTypeid();
					type = t.getType();
				}
			}
		}
		if(languageid.equals("") || typeid.equals(""))
		{
			for(int i =0;i<tlist.size();i++)
			{
				Tactics t = tlist.get(i);
				if(t.getSpid().equalsIgnoreCase(spId))
				{
					if(t.getToicp().equalsIgnoreCase(toicp))
					{
						if(t.getPaycode().indexOf("*")>-1)
						{
							if(code.toLowerCase().startsWith(t.getPaycode().replace("*", "").toLowerCase()))
							{
								languageid = t.getLanguageid();
								typeid = t.getTypeid();
								type = t.getType();
							}
						}
					}
				}
			}
		}
		if(languageid.equals("") || typeid.equals(""))
		{
			for(int i =0;i<tlist.size();i++)
			{
				Tactics t = tlist.get(i);
				if(t.getSpid().equalsIgnoreCase(spId))
				{
					if(t.getPaycode().indexOf("*")>-1)
					{
						if(code.toLowerCase().startsWith(t.getPaycode().replace("*", "").toLowerCase()))
						{
							languageid = t.getLanguageid();
							typeid = t.getTypeid();
							type = t.getType();
						}
					}
				}
			}
		}
		String  content="";
		String id = "";
		if(!languageid.equals("") && !typeid.equals(""))
		{
			for(int j = 0;j< list.size();j++)
			{
				Message m  = list.get(j);
				if(m.getLanguageId().equals(languageid) && m.getTypeId().equals(typeid))
				{
					newlist.add(m);
				}
				if(newlist.size()>0)
				{
					if(type.equals("2"))
					{
						content = newlist.get(0).getContent();
						id = newlist.get(0).getId();
					}
					else
					{
						int index=(int)(Math.random()*newlist.size());
						content = newlist.get(index).getContent();
						id = newlist.get(index).getId();
					}
				}
			}
		}
		if(content.indexOf("#url#")>-1)
		{
			content = content.replace("#url#",url+id);
		}
			return content;
	}
}
